﻿//using AzureServer.CoreService;
//using Dawn;
//using FluentValidation;
//using FluentValidation.Results;
//using Newtonsoft.Json.Linq;
//using System;
//using System.Collections.Generic;
//using System.Linq;

//namespace AzureServer.Test
//{
//    public class Validator
//    {
//        public JsonValidationErrorResult Validate(string key, string dataId, Dictionary<string, object> data, IEnumerable<JsonRow> jsonRows)
//        {
//            var errors = new List<JsonValidationErrorRow>();
//            foreach (var field in data)
//            {
//                var row = jsonRows.FirstOrDefault(x => x.Name == field.Key);

//                if (row == null) throw new ArgumentException($"Invalid property name: {field.Key}");

//                var ruleErrorList = ValidateByRules(field.Key, field.Value, row?.Type, row?.Validation); if
//                (ruleErrorList == null) continue;

//                errors.Add(new JsonValidationErrorRow
//                {
//                    FieldName = row.Name,
//                    Key = key,
//                    Validation =
//                ruleErrorList
//                });
//            }

//            if (!errors.Any()) return null;

//            return new JsonValidationErrorResult { DataId = dataId, Errors = errors };
//        }

//        private List<JsonValidationRuleError> ValidateByRules(string name, object value, string type,
//        IEnumerable<JsonFieldValidationRule> rules)
//        {
//            if (rules == null) return null;

//            if (rules.Any()) return null;

//            List<JsonValidationRuleError> errors = null; foreach (var rule in rules)
//            {
//                var vResult =
//ValidateByRule(name, value, type, rule);

//                if (vResult == null) continue; if (vResult.IsValid) continue;

//                if (errors == null) errors = new List<JsonValidationRuleError>();

//                foreach (var error in vResult.Errors)
//                {
//                    errors.Add(new JsonValidationRuleError
//                    {
//                        RuleName =
//error.ErrorCode,
//                        Message = string.IsNullOrWhiteSpace(rule.ErrorMessage) ? error.ErrorMessage :
//rule.ErrorMessage
//                    });
//                }
//            }

//            return errors;
//        }

//        private ValidationResult ValidateByRule(string name, object value, string type,
//        JsonFieldValidationRule rule)
//        {
//            var valueToCompare = rule.Parameters; return
//(rule.RuleName.ToLower()) switch
//{
//"required" => v_required(name, value),
//"type" => v_type(name,
//value, valueToCompare),
//"pattern" => v_pattern(name, value, valueToCompare),
//"max" => v_max(name,
//value, type, valueToCompare),
//"min" => v_min(name, value, type, valueToCompare),
//"length" =>
//v_length(name, value, type, valueToCompare),
//"maxLength" => v_length(name, value, type,
//valueToCompare, "maxLength"),
//"minLength" => v_length(name, value, type, valueToCompare,
//"minLength"),
//_ => null,
//};
//        }

//        private ValidationResult v_required(string name, object value)
//        {
//            var v = new
//InlineValidator<object>(); v.RuleFor(x => x).Empty().WithName(name).WithErrorCode("required");
//            return v.Validate(value);
//        }

//        private ValidationResult v_pattern(string name, object value, string valueToCompare)
//        {
//            var v =
//new InlineValidator<object>(); v.RuleFor(x => x).Empty().WithName(name).WithErrorCode("pattern");
//            v.RuleFor(x => x.ToString()).Matches(valueToCompare).WithName(name).WithErrorCode("pattern");
//            return v.Validate(value);
//        }

//        private ValidationResult v_type(string name, object value, string type)
//        {
//            var v = new
//InlineValidator<object>(); v.RuleFor(x => x).Empty().WithName(name).WithErrorCode("type");

//            if (string.IsNullOrEmpty(type)) return null; switch (type.ToLower())
//            {
//                case "string": return v.Validate(value);

//                case "integer":
//                    v.RuleFor(x => x).Must(x => int.TryParse(x.ToString(), out var
//    temp)).WithName(name).WithErrorCode("type:integer"); return v.Validate(value);

//                case "float":
//                    v.RuleFor(x => x).Must(x => float.TryParse(x.ToString(), out var
//      temp)).WithName(name).WithErrorCode("type:float"); return v.Validate(value);

//                case "number":
//                    v.RuleFor(x => x).Must(x => decimal.TryParse(x.ToString(), out var
//     temp)).WithName(name).WithErrorCode("type:number"); return v.Validate(value);

//                case "boolean":
//                    v.RuleFor(x => x).Must(x => bool.TryParse(x.ToString(), out var
//    temp)).WithName(name).WithErrorCode("type:boolean"); return v.Validate(value);

//                case "array":
//                    v.RuleFor(x => x).Must(x => JToken.Parse(x.ToString()) is
//      JArray).WithName(name).WithErrorCode("type:array"); return v.Validate(value);

//                case "object"://Must be of type object and not Array.isArray. v.RuleFor(x => x).Must(x =>
//                    JToken.Parse(x.ToString()) is JObject).WithName(name).WithErrorCode("type:object"); return v.Validate(value);

//                case "date":
//                    v.RuleFor(x => x).Must(x => DateTime.TryParse(x.ToString(), out var
//       temp)).WithName(name).WithErrorCode("type:date"); return v.Validate(value);

//                case "email":

//                    v.RuleFor(x => x.ToString()).EmailAddress().WithName(name).WithErrorCode("type:email"); return v.Validate(value);

//                case "url":
//                    v.RuleFor(x =>
//        x.ToString()).Matches(@"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$").WithName(name).WithErrorCode("type:url");
//                    return v.Validate(value);

//                case "any":
//                default:// Can be any type. return null; } }

//                    private ValidationResult v_length(string name, object value, string type, string valueToCompare,
//                    string builder = "length")
//                    {
//                        var v = new InlineValidator<object>(); v.RuleFor(x => x).Empty().WithName(name).WithErrorCode("length");

//                        if (int.TryParse(valueToCompare, out var length))
//                        {
//                            switch (builder)
//                            {
//                                case "maxLength":
//                                    v.RuleFor(x => (string)x).MaximumLength(length).WithName(name).WithErrorCode(builder); break;

//                                case "minLength":
//                                    v.RuleFor(x =>
//                  (string)x).MinimumLength(length).WithName(name).WithErrorCode(builder); break;

//                                default: v.RuleFor(x => (string)x).Length(length).WithName(name).WithErrorCode(builder); break;
//                            }
//                        }
//                        else { v.RuleFor(x => (string)x).Must(x => false).WithName(name).WithErrorCode("length"); }
//                        return v.Validate(value);
//                    }

//                    private ValidationResult v_min(string name, object value, string type, string valueToCompare)
//                    {
//                        var v = new InlineValidator<object>(); v.RuleFor(x => x).Empty().WithName(name).WithErrorCode("min");

//                        switch (type)
//                        {
//                            case "number":
//                                if (double.TryParse(valueToCompare, out var number))
//                                {
//                                    v.RuleFor(x
//=> (double)x).GreaterThan(number).WithName(name).WithErrorCode("min");
//                                }
//                                return v.Validate(value);

//                            case "date":
//                                if (DateTime.TryParse(valueToCompare, out var data))
//                                {
//                                    v.RuleFor(x =>
//(DateTime)x).GreaterThan(data).WithName(name).WithErrorCode("min");
//                                }
//                                return v.Validate(value);

//                            default:

//                                v.RuleFor(x => x).Must(x => false).WithName(name).WithErrorCode("min");

//                                return v.Validate(value);
//                        }
//                    }

//                    private ValidationResult v_max(string name, object value, string type, string valueToCompare)
//                    {
//                        var v = new InlineValidator<object>(); v.RuleFor(x =>
//                        x).Empty().WithName(name).WithErrorCode("min"); switch (type)
//                        {
//                            case "number":
//                                if
//(double.TryParse(valueToCompare, out var number))
//                                {
//                                    v.RuleFor(x =>
//(double)x).LessThan(number).WithName(name).WithErrorCode("max");
//                                }
//                                return v.Validate(value);

//                            case "date":
//                                if (DateTime.TryParse(valueToCompare, out var date))
//                                {
//                                    v.RuleFor(x =>
//(DateTime)x).LessThan(date).WithName(name).WithErrorCode("max");
//                                }
//                                return v.Validate(value);

//                            default:
//                                v.RuleFor(x => x).Must(x => false).WithName(name).WithErrorCode("max"); return
//                       v.Validate(value);
//                        }
//                    }

//                    private ValidationResult v_validator(string name, object value, string type, string
//                    valueToCompare)
//                    {
//                        var v = new InlineValidator<object>(); v.RuleFor(x => x).Empty().WithName(name).WithErrorCode("min");

//                        v.RuleFor(x => x).Must(x => true);
//                        return v.Validate(value);
//                    }
//            }
//        }